package io.github.talelin.latticy.controller.v1;


import io.github.talelin.latticy.annotation.AccessLimit;
import io.github.talelin.latticy.annotation.ApiIdempotent;
import io.github.talelin.latticy.bo.Student;
import io.github.talelin.latticy.common.pojo.Mail;
import io.github.talelin.latticy.model.ProductBean;
import io.github.talelin.latticy.model.es.Blog;
import io.github.talelin.latticy.service.StudentService;
import io.github.talelin.latticy.service.TestService;
import io.github.talelin.latticy.service.TokenService;
//import io.github.talelin.latticy.service.impl.RuleService;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.UpdateQuery;
import org.springframework.data.elasticsearch.core.query.UpdateResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @authoer:wulongbo
 * @createDate:2022/7/9
 * @description:
 */

@RestController
@RequestMapping("/test")
public class HelloController {


    @Autowired
    private TestService testService;

//    @Autowired
//    private RuleService ruleService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private StudentService studentService;

//    @Autowired
//    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @RequestMapping("/product")
    public String product() {
        Student student = new Student();
        student.setScore(90);
        student.setName("张三");
        studentService.send(student);
        return "OK";
    }

//    @RequestMapping("/rule")
//    public String rule() {
//        ruleService.rule();
//        return "OK";
//    }

    @ApiIdempotent
    @PostMapping("/testIdempotence")
    public String testIdempotence() {
        return "success";
    }

    @AccessLimit(maxCount = 5, seconds = 5)
    @PostMapping("/accessLimit")
    public String accessLimit() {
        return "accessLimit success";
    }

    @PostMapping("/send")
    public void sendMail(@Validated @RequestBody Mail mail) {
        testService.send(mail);
    }

    @GetMapping("/token")
    public String token() {
        return tokenService.createToken();
    }

//    @PostMapping("createIndex")
//    public String createIndex() {
//        IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(Blog.class);
//        // 是否存在，存在则删除
//        if (indexOperations.exists()) {
//            indexOperations.delete();
//        }
//        // 创建索引
//        indexOperations.create();
//        // createMapping 根据实体类获取映射关系
//        // putMapping 把映射关系添加到索引中
//        Document mapping = indexOperations.createMapping(Blog.class);
//        indexOperations.putMapping(mapping);
//        return "success";
//    }
//
//    @PostMapping("deleteIndex")
//    public String deleteIndex() {
//        // 删除索引，会根据Blog类的@Document注解信息来删除
//        IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(Blog.class);
//        boolean delete = indexOperations.delete();
//        System.out.println("删除索引:" + delete);
//        return "success";
//    }
//
//    @PostMapping("addDocument")
//    public Blog addDocument() {
//        Long id = 1L;
//        Blog blog = new Blog();
//        blog.setBlogId(id);
//        blog.setTitle("Spring Data ElasticSearch学习教程" + id);
//        blog.setContent("这是添加单个文档的实例" + id);
//        blog.setAuthor("Tony");
//        blog.setCategory("ElasticSearch");
//        blog.setCreateTime(new Date());
//        blog.setStatus(1);
//        blog.setSerialNum(id.toString());
//        return elasticsearchRestTemplate.save(blog);
//    }
//
//    @PostMapping("addDocuments")
//    public Object addDocuments(Integer count) {
//        List<Blog> blogs = new ArrayList<>();
//        for (int i = 1; i <= count; i++) {
//            Long id = (long)i;
//            Blog blog = new Blog();
//            blog.setBlogId(id);
//            blog.setTitle("Spring Data ElasticSearch学习教程" + id);
//            blog.setContent("这是添加单个文档的实例" + id);
//            blog.setAuthor("Tony");
//            blog.setCategory("ElasticSearch");
//            blog.setCreateTime(new Date());
//            blog.setStatus(1);
//            blog.setSerialNum(id.toString());
//            blogs.add(blog);
//        }
//        return elasticsearchRestTemplate.save(blogs);
//    }
//
//    /**
//     * 跟新增是同一个方法。若id已存在，则修改。
//     * 无法只修改某个字段，只能覆盖所有字段。若某个字段没有值，则会写入null。
//     * @return 成功写入的数据
//     */
//    @PostMapping("editDocument")
//    public Blog editDocument() {
//        Long id = 1L;
//        Blog blog = new Blog();
//        blog.setBlogId(id);
//        blog.setTitle("Spring Data ElasticSearch学习教程" + id);
//        blog.setContent("这是修改单个文档的实例" + id);
//
//        return elasticsearchRestTemplate.save(blog);
//    }
//
//    @PostMapping("editDocumentPart")
//    public UpdateResponse editDocumentPart() {
//        long id = 1L;
//        Document document = Document.create();
//        document.put("title", "修改后的标题" + id);
//        document.put("content", "修改后的内容" + id);
//        UpdateQuery updateQuery = UpdateQuery.builder(Long.toString(id))
//                .withDocument(document)
//                .build();
//        UpdateResponse response = elasticsearchRestTemplate.update(updateQuery, IndexCoordinates.of("blog"));
//        return response;
//    }
//
//    @PostMapping("editDocumentsPart")
//    public void editDocumentsPart(int count) {
//        List<UpdateQuery> updateQueryList = new ArrayList<>();
//        for (int i = 1; i <= count; i++) {
//            long id = (long) i;
//            Document document = Document.create();
//            document.put("title", "修改后的标题" + id);
//            document.put("content", "修改后的内容" + id);
//            UpdateQuery updateQuery = UpdateQuery.builder(Long.toString(id))
//                    .withDocument(document)
//                    .build();
//            updateQueryList.add(updateQuery);
//        }
//        elasticsearchRestTemplate.bulkUpdate(updateQueryList, IndexCoordinates.of("blog"));
//    }
//
//    @GetMapping("findById")
//    public Blog findById(Long id) {
//        return elasticsearchRestTemplate.get(
//                id.toString(), Blog.class, IndexCoordinates.of("blog"));
//    }
//
//    @PostMapping("deleteDocumentById")
//    public String deleteDocumentById(Long id) {
//        return elasticsearchRestTemplate.delete(id.toString(), Blog.class);
//    }
//
//    @PostMapping("deleteDocumentByQuery")
//    public void deleteDocumentByQuery(String title) {
//        NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()
//                .withQuery(QueryBuilders.matchQuery("title", title))
//                .build();
//
//        elasticsearchRestTemplate.delete(nativeSearchQuery, Blog.class, IndexCoordinates.of("blog"));
//    }
//
//    @PostMapping("deleteDocumentAll")
//    public void deleteDocumentAll() {
//        NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()
//                .withQuery(QueryBuilders.matchAllQuery())
//                .build();
//        elasticsearchRestTemplate.delete(nativeSearchQuery, Blog.class, IndexCoordinates.of("blog"));
//    }

}
